package com.project.dao.impl;

import com.project.bean.ClassBean;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.dao.IClassDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@Repository
public class ClassDaoImpl extends BaseDao implements IClassDao {
    @Override
    public List<ClassBean> findAll() {
        List<ClassBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_class");
            rs =ps.executeQuery();
            while (rs.next()){
                list.add(this.getClassBean());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return list;
    }

    @Override
    public void del(Integer id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete from t_class where pk_classId=?");
            ps.setObject(1,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void add(ClassBean classBean) {
        this.setConnection();
        try {
            ps = con.prepareStatement("insert into t_class(t_className,t_classTeacher,t_classDate) values (?,?,?)");
            ps.setObject(1,classBean.getClassName());
            ps.setObject(2,classBean.getTeacher());
            ps.setObject(3,classBean.getClassDate());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    public ClassBean getClassBean(){
        ClassBean classBean = new ClassBean();
        try {
            classBean.setClassName(rs.getString("t_className"));
            classBean.setId(rs.getInt("pk_classId"));
            classBean.setTeacher(rs.getString("t_classTeacher"));
            String bir = rs.getString("t_classDate");
            classBean.setClassDate(bir!=null? LocalDate.parse(bir):null);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return classBean;
    }

//    public static void main(String[] args) {
//        IClassDao dao = new ClassDaoImpl();
////        System.out.println(dao.findAll());
////        dao.add(new ClassBean("王","wan"));
//        dao.del(3);
//    }
}
